<?php
/**
 * CouponController.class.php
 * 优惠控制器
 *
 * @author   <王中艺>
 * @date    2017-02-19
 */

namespace Admin\Controller;

class CouponController extends BaseController{
	/**
	 * 优惠码列表获取
	 * @param   $p 页码数
	 * @return  $list
	 */
	function getCodeList(){
		$page 	= $this->iInfo['p'] ? $this->iInfo['p'] : 1;
		$pageHtml	 = 0;
		$list 	= D('Coupon')->getCodeList($page, $pageHtml);
		$this->outOInfo(array('list'=>$list, 'page_html'=>$pageHtml));
	}

	/**
	 * 生成优惠码
	 * @param   $end_date 截止日期
	 * @param   $amount 抵用金额
	 * @return  boolean
	 */
	function createCouponCode(){
		$this->unEmptyParam(array('end_date', 'amount', 'limit_amount', 'code'));
		$endTime 	= $this->iInfo['end_date'] ? strtotime($this->iInfo['end_date']) : 0;
		$amount 	= $this->iInfo['amount'];
		$limitAmount = $this->iInfo['limit_amount'];
		// $code  	= create_coupon_code();
		$code 	= strtoupper($this->iInfo['code']);

		$result 	= D('Coupon')->addCode($code, $amount, $limitAmount, $endTime);
		if($result === false)
			$this->outOInfo(D('Coupon')->errMsg, 0);
		$this->outOInfo('Oh yes!');
	}

	/**
	 * 更改优惠码状态
	 * @param   $code_id 优惠码ID
	 * @param   $status 状态吗
	 * @return  boolean
	 */
	function changeCodeStatus(){
		$this->unEmptyParam(array('code_id', 'status'));
		$codeId 	= $this->iInfo['code_id'];
		$status 	= $this->iInfo['status'];

		$result 	= D('Coupon')->changeCodeStatus($codeId, $status);
		if($result === false)
			$this->outOInfo(D('Counpon')->errMsg, 0);

		$this->outOInfo('Oh yes!');
	}

	/**
	 * 优惠码使用记录
	 * @param 	$dateS 	开始时间
	 * @param 	$dateE 	结束时间
	 * @return 	$list
	 */
	function getUseLog(){
		$timeS 	= $this->iInfo['date_start'] ? strtotime($this->iInfo['date_start']) : false;
		$timeE 	= $this->iInfo['date_end']	? strtotime($this->iInfo['date_end'].' +1 day') : false;
		$page 	= $this->iInfo['p'] ? $this->iInfo['p'] : 1;

		$total 	= true;
		$list 	= D('Coupon')->getUseLog($timeS, $timeE, $page, $pageHtml, $total);
		if($list === false)
			$this->outOInfo(D('Coupon')->errMsg, 0);

		$this->outOInfo(array('list' => $list, 'page_html' => $pageHtml, 'total' => $total));
	}

	/**
	 * 导出使用记录
	 * @param 	$start 	开始位置
	 * @param 	$length 	长度
	 * @param 	$date_start 	开始日期
	 * @param 	$date_end 结束日期
	 * @return 	excel_file or count
	 */
	function outExcelUseLog(){
		$start 	= isset($this->iInfo['start']) ? $this->iInfo['start'] : false;
		$length 	= $this->iInfo['length'] ? $this->iInfo['length'] : 100;
		$timeS 	= $this->iInfo['date_start'] ? 
			strtotime($this->iInfo['date_start']) : false;
		$timeE 	= $this->iInfo['date_end'] ? 
			strtotime($this->iInfo['date_end'].' +1 day') : false;

		$data 	= D('Coupon')->outExcelUseLog($start, $length, $timeS, $timeE);
		if($start === false)
			$this->outOInfo(array('count' => $data));

		$title 	= array(
			'记录ID', '用户ID', '用户昵称', '真实姓名', 
			'优惠码值', '抵扣金额', '使用时间'
		);
		foreach($data as &$item){
			$i['id']	= $item['id'];
			$i['user_id']	= $item['user_id'];
			$i['nickname']	= $item['nickname'];
			$i['truename'] 	= $item['truename'];
			$i['code']	= $item['code'];
			$i['amount']	= $item['amount'];
			$i['use_date']	= date('Y-m-d H:i:s', $item['use_time']);
			$item 	= $i;
		}
		out_excel($title, $data, 'code_use_log_'.$start);
	}

	function debug(){
		for($i=0; $i < 10; $i++){
			$code 	= create_coupon_code();
			$amount 	= 20;
			$limitAmount 	= 200;
			$endTime 	= strtotime('2017-03-31');
			$result 	= D('Coupon')->addCode($code, $amount, $limitAmount, $endTime);
			dump($result);
		}
	}
}